<template>
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区域 -->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addGoods">添加商品</el-button>
                </el-col>
            </el-row>
            <!-- tab表格区域 -->
            <el-table :data="goodsList" stripe border style="width: 100%">
                <el-table-column type="index" width="180"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="180"></el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)" width="180"></el-table-column>
                <el-table-column prop="goods_weight" label="商品重量" width="180"></el-table-column>
                <el-table-column prop="add_time" label="创建时间" width="180">
                    <template slot-scope="scope">
                        {{scope.row.add_time | dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteGoodsList(scope.row.goods_id)">
                            删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                //商品列表的查询参数
                queryInfo: {
                    query: '',
                    pagenum: 1,
                    pagesize: 10,
                },
                //存放商品列表数据
                goodsList: [],
                //存放商品列表数据的条数
                total: 0,

            }
        },
        created() {
            this.getGoodsList();

        },
        methods: {
            //获取商品列表数据
            async getGoodsList() {
                const {
                    data: res
                } = await this.$http.get('goods', {
                    params: this.queryInfo
                });
                console.log(res);
                if (res.meta.status !== 200) {
                    return this.$message.error('获取商品列表数据失败')
                }
                this.goodsList = res.data.goods;
                this.total = res.data.total;
            },
            //监听每页显示多少条数据改变的事件
            handleSizeChange(newsize) {
                this.queryInfo.pagesize = newsize;
                this.getGoodsList();

            },
            //监听页码值改变的事件
            handleCurrentChange(newpage) {
                this.queryInfo.pagenum = newpage;
                this.getGoodsList();

            },
            //点击删除按钮触发的函数
            async deleteGoodsList(id) {
                const result =await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(error => {
                    return error
                })
                console.log(result);
                if(result!=='confirm'){
                    return this.$message.info('用户取消了删除')
                }
                //发起删除的请求
                const {data:res}=await this.$http.delete('goods/'+id);
                console.log(res);
                if(res.meta.status!==200){
                    return this.$message.error('删除失败')
                }
                this.$message.success('删除成功');
                this.getGoodsList();
            },
            //点击添加商品按钮，触发的事件
            addGoods(){
                this.$router.push('/goods/Add');
            }



        }
    }
</script>
<style lang="less" scoped>

</style>